using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Assertions.Must;

/**相机跟随 */
public class CameraFollow : MonoBehaviour
{
    public Transform target;
    public float smoothing;

    //相机移动的范围
    public Vector2 minPosition;
    public Vector2 maxposition;

    public static CameraFollow instance;

    private void Awake()
    {
        instance = this;
    }

    void LateUpdate()
    {
        if (!target || (Math.Abs(transform.position.x - target.position.x) < 0.001 &&
                        Math.Abs(transform.position.y - target.position.y) < 0.001))
        {
            return;
        }
        Vector3 targetPos = target.position;
        
        //限制移动相机的最大最小
        targetPos.x = Mathf.Clamp(targetPos.x, minPosition.x, maxposition.x);
        targetPos.y = Mathf.Clamp(targetPos.y, minPosition.y, maxposition.y);
        
        //相机跟随移动
        transform.position = Vector3.Lerp(transform.position, targetPos, smoothing);//进行线性插值函数
    }

    /**设置目标 */
    public void SetTarget(Transform tar)
    {
        target = tar;
    }
    
}
